package com.yyx.demo1.controller;

import com.yyx.demo1.entity.Result;
import com.yyx.demo1.entity.User;

import com.yyx.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    public UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User data=userService.login(user);
        Result result;
        if(data!=null) {
            result = new Result(200, "登录成功", data);
        }else {
            result = new Result(400, "账号密码错误", data);
        }
        return result;
    }

    @PostMapping("/register")
    public Result Register(@RequestBody User user){
        Result result = null;
        int count=0;
        try {
            count = userService.register(user);

        }catch (Exception exception) {
            System.out.println(count);
            System.out.println(exception.getMessage());
        }
        if(count>0) {
            result = new Result(200, "注册成功");
        }else {
            result = new Result(400, "注册失败");
        }
        return result;

    }
}
